/*
 * EditHistoryDialog.java
 *
 * Created on __DATE__, __TIME__
 */

package ui;

import java.awt.Dimension;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import bean.ConnectionBean;

/**
 *
 * @author  __USER__
 */
public class EditHistoryDialog extends javax.swing.JDialog {

	/** Creates new form EditHistoryDialog */
	public EditHistoryDialog(java.awt.Frame parent, boolean modal,
			String phoneNumber) {
		super(parent, modal);
		this.phoneNumber = phoneNumber;
		initComponents();
		setPreferredSize(new Dimension(700, 500));
		fillTableDate();
	}

	public void fillTableDate() {
		try {
			rowData.clear();
			Connection con = ConnectionBean.getConnection();
			PreparedStatement psm = con
					.prepareStatement("select * from dignosis where phone_no = ?");
			psm.setString(1, phoneNumber);
			ResultSet rs = psm.executeQuery();
			while (rs.next()) {
				Vector<String> row = new Vector<String>();
				row.add(rs.getString("patient_name"));
				row.add(rs.getString("phone_no"));
				row.add(rs.getString("reference_id"));
				row.add(rs.getString("dignosis_date"));
				row.add(rs.getString("disease"));
				row.add(rs.getString("dignosis"));
				row.add(rs.getString("prescription"));
				rowData.add(row);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		((DefaultTableModel) idHistoryTable.getModel()).fireTableDataChanged();
	}

	Vector<String> columns = new Vector<String>();
	Vector<Vector<String>> rowData = new Vector<Vector<String>>();
	private String phoneNumber;

	class historyListTableModel extends DefaultTableModel {
		public historyListTableModel() {
			columns.addElement("Name");
			columns.addElement("ID/Ph.No.");
			columns.addElement("Reference_id");
			columns.addElement("Date");
			columns.addElement("Disease");
			columns.addElement("Dignosis");
			columns.addElement("Prescription");
		}

		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return String.class;
		}

		@Override
		public int getColumnCount() {
			return columns.size();
		}

		@Override
		public String getColumnName(int columnIndex) {
			return columns.elementAt(columnIndex);
		}

		@Override
		public int getRowCount() {
			return rowData.size();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return rowData.elementAt(rowIndex).elementAt(columnIndex);
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			rowData.elementAt(rowIndex).setElementAt(String.valueOf(aValue),
					columnIndex);
		}

		@Override
		public void fireTableDataChanged() {
			super.fireTableDataChanged();
		}

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		idHistoryTable = new javax.swing.JTable();
		idEditHistory = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setText("Patient History");

		idHistoryTable.setModel(new historyListTableModel());
		jScrollPane1.setViewportView(idHistoryTable);

		idEditHistory.setText("Edit History");
		idEditHistory.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				idEditHistoryActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jScrollPane1,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				561,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				idEditHistory))
														.addComponent(
																jLabel1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																112,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel1)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																idEditHistory)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(51, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void idEditHistoryActionPerformed(java.awt.event.ActionEvent evt) {
		if(isRowSelectionValid()){
			String refId = idHistoryTable.getValueAt(idHistoryTable.getSelectedRow(), 2).toString();
			UpdateHistoryDialog updateHistoryDialog = new UpdateHistoryDialog(null, true, refId, this);
			updateHistoryDialog.setVisible(true);
		}
	}
	
	private boolean isRowSelectionValid() {
		int rows = idHistoryTable.getSelectedRowCount();
		if (rows > 1)
			JOptionPane.showMessageDialog(this,
					"Please select only one row to Edit");
		else if (rows < 1)
			JOptionPane
					.showMessageDialog(this, "Please select one row to Edit");
		else
			return true;
		return false;
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton idEditHistory;
	private javax.swing.JTable idHistoryTable;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables

}